package com.zyw.service.clothing.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.Clothing;
import com.zyw.entity.PersonnelStaff;
import com.zyw.enums.ManagermentSystemEnum;
import com.zyw.exception.ManagermentSystemException;
import com.zyw.mapper.clothing.ClothingMapper;
import com.zyw.service.clothing.ClothingService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ClothingServiceImpl implements ClothingService {

    @Resource
    public ClothingMapper clothingMapper;


    @Override
    public LayUITableVO<List<Clothing>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<Clothing> list = clothingMapper.findPage(map);
        for (Clothing clothing : list) {
            convert(clothing);
        }


        PageInfo<Clothing> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<Clothing>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());
        return vo;
    }

    @Override
    public Clothing selectById(String id) {

        return clothingMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Clothing clothing) {
        convertContrary(clothing);

        clothingMapper.updateByPrimaryKeySelective(clothing);

    }

    @Override
    @Transactional
    public void save(Clothing clothing) {
        convertContrary(clothing);

        clothingMapper.insertSelective(clothing);
    }

    @Override
    public void checkId(String id) {
        Clothing clothing = clothingMapper.selectByPrimaryKey(id);
        if (clothing != null){
            throw new ManagermentSystemException(ManagermentSystemEnum.CLOTHING_ID_REPETITION);
        }
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        clothingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Clothing> findAllIdAndName() {
        return clothingMapper.findAllIdAndName();
    }

    private void convert(Clothing clothing) {
        String type = "";
        if ("1".equals(clothing.getS())){
            type+="S /";
        }
        if("1".equals(clothing.getM())){
            type+=" M /";
        }
        if ("1".equals(clothing.getL())){
            type+=" L /";
        }
        if ("1".equals(clothing.getXl())){
            type+=" XL /";
        }
        if("1".equals(clothing.getXxl())){
            type+=" 2XL /";
        }
        if ("1".equals(clothing.getXxxl())){
            type+=" 3XL /";
        }
        if ("1".equals(clothing.getXxxxl())){
            type+=" 4XL /";
        }
        if (type.length()>0){
            type = type.substring(0,type.lastIndexOf("/"));
        }

        clothing.setType(type);

    }

    private void convertContrary(Clothing clothing){

        if ("on".equals(clothing.getS())){
            clothing.setS("1");
        }else {
            clothing.setS("0");
        }
        if ("on".equals(clothing.getM())){
            clothing.setM("1");
        }else {
            clothing.setM("0");
        }
        if ("on".equals(clothing.getL())){
            clothing.setL("1");
        }else {
            clothing.setL("0");
        }
        if ("on".equals(clothing.getXl())){
            clothing.setXl("1");
        }else {
            clothing.setXl("0");
        }
        if ("on".equals(clothing.getXxl())){
            clothing.setXxl("1");
        }else {
            clothing.setXxl("0");
        }
        if ("on".equals(clothing.getXxxl())){
            clothing.setXxxl("1");
        }else {
            clothing.setXxxl("0");
        }
        if ("on".equals(clothing.getXxxxl())){
            clothing.setXxxxl("1");
        }else {
            clothing.setXxxxl("0");
        }

    }
}
